package cn.platform.service.impl;

import cn.platform.entity.Activity;
import cn.platform.entity.SchAnnounce;
import cn.platform.mapper.ActivityAdminMapper;
import cn.platform.mapper.ClazzMapper;
import cn.platform.mapper.TeacherMapper;
import cn.platform.request.ActivityQueryDTO;
import cn.platform.request.ActivityUpdateDTO;
import cn.platform.response.ActivityVO;
import cn.platform.response.PageResult;
import cn.platform.response.SchAnnounceVO;
import cn.platform.response.UserStudentVO;
import cn.platform.service.ActivityService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityAdminMapper activityAdminMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 根据条件分页查询活动信息
     * @param activityQueryDTO
     * @return
     * @author oyq
     */
    @Override
    public PageResult<ActivityVO> page(ActivityQueryDTO activityQueryDTO) {

        if (activityQueryDTO.getGrade()!=null){
            int currentYear = Year.now().getValue();
            activityQueryDTO.setGradeYear(currentYear-activityQueryDTO.getGrade()+1);
        }

        PageHelper.startPage(activityQueryDTO.getPage(),activityQueryDTO.getPageSize());
        Map<String,Object> map = new HashMap<>();
        if (activityQueryDTO.getTitle()!=null && !activityQueryDTO.getTitle().isEmpty()){
            map.put("title",activityQueryDTO.getTitle());
        }else {
            map.put("title",null);
        }
        if(activityQueryDTO.getPublisher()!=null && !activityQueryDTO.getPublisher().isEmpty()){
            map.put("publisher",activityQueryDTO.getPublisher());
        }else {
            map.put("publisher",null);
        }
        activityQueryDTO.setType("活动");
        map.put("type",activityQueryDTO.getType());
        if(activityQueryDTO.getStartTime()!=null){
            map.put("startTime", LocalDateTime.of(activityQueryDTO.getStartTime(), LocalTime.MIN));
        }else {
            map.put("startTime",null);
        }
        if(activityQueryDTO.getEndTime()!=null){
            map.put("endTime",LocalDateTime.of(activityQueryDTO.getEndTime(), LocalTime.MAX));
        }else {
            map.put("endTime",null);
        }
        if(activityQueryDTO.getGradeYear()!=null){
            map.put("gradeYear", activityQueryDTO.getGradeYear());
        }
        if(activityQueryDTO.getClassNum()!=null){
            map.put("classNum",activityQueryDTO.getClassNum());
        }
        List<ActivityVO> voList =activityAdminMapper.pageQuery(map);

        int currentYear = Year.now().getValue();
        for(ActivityVO vo:voList){
            int gradeNum = currentYear-vo.getGradeYear()+1;
            if (gradeNum>=1 && gradeNum<=6){
                vo.setGrade(gradeNum);
            }else {
                vo.setGrade(null);
                vo.setClassNum(null);
            }
        }

        Page<ActivityVO> page =(Page<ActivityVO>) voList;

        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 查看活动详情
     * @param id
     * @return
     * @author oyq
     */
    @Override
    public ActivityVO detail(String id) {
        Activity activity = activityAdminMapper.getById(id);

        String publisher = null;
        publisher=teacherMapper.getNameById(activity.getPubId());
        String classNum=null;
        classNum=clazzMapper.getClassNumById(activity.getClassID());
        int currentYear = Year.now().getValue();
        int grade = currentYear-clazzMapper.getGradeYearById(activity.getClassID())+1;

        ActivityVO  activityVO = new ActivityVO();
        BeanUtils.copyProperties(activity,activityVO);

        LocalDate pubTime=activity.getPubTime().toLocalDate();
        LocalDate updateTime = activity.getUpdateTime().toLocalDate();
        activityVO.setPubTime(pubTime);
        activityVO.setUpdateTime(updateTime);

        activityVO.setPublisher(publisher);
        activityVO.setClassNum(classNum);
        activityVO.setGrade(grade);
        return activityVO;
    }

    /**
     * 更新活动
     * @param dto
     */
    @Override
    public void update(ActivityUpdateDTO dto) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(dto,activity);
        activity.setUpdateTime(LocalDateTime.now());
        activityAdminMapper.updateActivity(activity);
    }

    /**
     * 删除单条活动记录
     * @param id
     */
    @Override
    public void delete(Integer id) {
        activityAdminMapper.deleteById(id);
    }

    /**
     * 批量删除活动
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        activityAdminMapper.deleteBatch(ids);
    }
}
